package com.Ngbussiness.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.Ngbussiness.dao.modul_categoryDao;
import com.Ngbussiness.util.Calenda;
import com.Ngbussiness.util.DatabaseAccess;
import com.Ngbussiness.util.EasyMapsManager;

public class modul_categoryDaoImpl implements modul_categoryDao{

	public List getcg3(int category1){
		DatabaseAccess dao=new DatabaseAccess();
		String sql="select Id,name from modul_category where category_id=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(category1);
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
}
	public JSONArray getcg1(){
		String sql ="select Id,name from category where modul=3;";
        try{
             DatabaseAccess databaseAccess = new DatabaseAccess(false);
             EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
             List list = easyMapsManager.executeQuery(sql);
             JSONArray json4 = new JSONArray();
             for(int k=0;k<list.size();k++){
            	 JSONObject json1=new JSONObject();
            	 JSONArray json2 = new JSONArray();
            	 HashMap adu=(HashMap)list.get(k);
            	 String name=adu.get("name").toString();
            	 int Id=Integer.parseInt(adu.get("Id").toString());
            	 System.out.println(name);
            	 json1.put("first_name", name);
            	 List list2=new ArrayList();
            	 String sql2="select name from modul_category where category_id=?;";
            	 DatabaseAccess databaseAccess1 = new DatabaseAccess(false);
				 EasyMapsManager easyMapsManager1 = new EasyMapsManager(databaseAccess1);
				 easyMapsManager1.setPreparedParameter(Id);
				 list2=easyMapsManager1.executeQuery(sql2);
				 for(int j=0;j<list2.size();j++){
					 HashMap adu1=(HashMap)list2.get(j);
                	 String name1=adu1.get("name").toString();
                	 json2.add(name1);
				 }
				 json1.put("second_mcid",json2);
				 json4.add(json1);
             }
             
             
//             JSONObject json3=new JSONObject();
//             for(int i=0;i<list.size();i++){
//            	 JSONArray json2=new JSONArray();
//            	 HashMap adu=(HashMap)list.get(i);
//            	 
//            	 System.out.println(Id);
//            	
//				   json4.add(json2);
//				 System.out.println("json2"+json2);
//             }
//             json3.put("first", json1);
//             json3.put("second",json4);
//             System.out.println(json3);
//            // messagelist.add(json4);
//             System.out.println("json1"+json1);
//             System.out.println("json4"+json4);
             databaseAccess.close();
             return json4;
            }catch(Exception e) {
                e.printStackTrace();
            }
             return null;
	}
	
	public int getcg1Id(String category){
		String sql="select Id from category where name like ?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(category);
			List list = easyMapsManager.executeQuery(sql);
			int Id=0;
			if(list.size()>0){
			HashMap adu=(HashMap)list.get(0);
			Id=Integer.parseInt(adu.get("Id").toString());
			}
			databaseAccess.close();
			System.out.println(Id);
			return Id;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public List getcg(){
		String sql="select Id,name from category where modul=3;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			List list = easyMapsManager.executeQuery(sql);
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
